package com.anolesoft.yr.mbus.base.service.impl;

import com.anolesoft.yr.mbus.base.dto.BaseContactDTO;
import com.anolesoft.yr.mbus.base.entity.BaseContactDO;
import com.anolesoft.yr.mbus.base.mapper.BaseContactMapper;
import com.anolesoft.yr.mbus.base.service.BaseContactService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.CustomBeanUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/9 14:54
 */
@Service("baseContactServiceImpl")
public class BaseContactServiceImpl implements BaseContactService {
    @Autowired
    private BaseContactMapper baseContactMapper;

    @Transactional
    @Override
    public int create(BaseContactDTO baseContactDTO) {
        return baseContactMapper.insert(BaseContactDO.builder()
                .objuid(baseContactDTO.getObjuid())
                .postcode(baseContactDTO.getPostcode())
                .contactNumber(baseContactDTO.getContactNumber())
                .crmAddress(baseContactDTO.getCrmAddress())
                .contactPerson(baseContactDTO.getContactPerson())
                .targetUid(baseContactDTO.getTargetUid())
                .type(baseContactDTO.getType())
                .factoryCode(baseContactDTO.getFactoryCode())
                .modifyUserUid(baseContactDTO.getModifyUserUid())
                .remark(baseContactDTO.getRemark())
                .createUserUid(baseContactDTO.getCreateUserUid())
                .build());
    }

    @Transactional
    @Override
    public int modify(BaseContactDTO baseContactDTO) {
        BaseContactDO baseContactDO = baseContactMapper.selectOneById(baseContactDTO.getObjuid());
        if (baseContactDO == null) {
            baseContactDO = BaseContactDO.builder().build();
        }
        baseContactDO.setContactNumber(baseContactDTO.getContactNumber());
        baseContactDO.setContactPerson(baseContactDTO.getContactPerson());
        baseContactDO.setCrmAddress(baseContactDTO.getCrmAddress());
        baseContactDO.setFactoryCode(baseContactDTO.getFactoryCode());
        baseContactDO.setModifyUserUid(baseContactDTO.getModifyUserUid());
        baseContactDO.setTargetUid(baseContactDTO.getTargetUid());
        baseContactDO.setType(baseContactDTO.getType());
        baseContactDO.setFactoryCode(baseContactDTO.getFactoryCode());
        baseContactDO.setRemark(baseContactDTO.getRemark());
        baseContactDO.setPostcode(baseContactDTO.getPostcode());
        return baseContactMapper.update(baseContactDO);
    }

    @Transactional
    @Override
    public int remove(String id) {
        return baseContactMapper.delete(id);
    }

    @Transactional
    @Override
    public int removeBatch(List<String> ids) {
        int res = 0;
        for (String id : ids) {
            res += baseContactMapper.delete(id);
        }
        return res;
    }

    @Override
    public List<BaseContactDTO> selectAll() {
        return baseContactMapper.selectAll().stream().map(t -> CustomBeanUtils.baseContactDO2DTO(t)).collect(Collectors.toList());
    }

//    private BaseContactDTO do2dto(BaseContactDO t) {
//        return BaseContactDTO.builder()
//                .contactNumber(t.getContactNumber())
//                .contactPerson(t.getContactPerson())
//                .createTime(t.getCreateTime())
//                .createUserUid(t.getCreateUserUid())
//                .crmAddress(t.getCrmAddress())
//                .deleteFlag(t.getDeleteFlag())
//                .factoryCode(t.getFactoryCode())
//                .modifyTime(t.getModifyTime())
//                .modifyUserUid(t.getModifyUserUid())
//                .objuid(t.getObjuid())
//                .postcode(t.getPostcode())
//                .remark(t.getRemark())
//                .targetUid(t.getTargetUid())
//                .type(t.getType())
//                .build();
//    }

    @Override
    public PageData<List<BaseContactDTO>> selectAll(Integer limit, Integer curr) {
        //return baseContactMapper.selectAll(new Page<>(curr, limit)).stream().map(t -> do2dto(t)).collect(Collectors.toList());
        Page<BaseContactDO> page = new Page<>(curr, limit);
        List<BaseContactDTO> baseContactDTOS = baseContactMapper.selectPageAll(page).stream().map(t -> CustomBeanUtils.baseContactDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>((int)page.getTotal(), baseContactDTOS);
    }

    @Override
    public Optional<BaseContactDTO> selectOneById(String id) {
        BaseContactDO baseContactDO = baseContactMapper.selectOneById(id);
        if (baseContactDO != null) {
            return Optional.of(CustomBeanUtils.baseContactDO2DTO(baseContactDO));
        }
        return Optional.empty();
    }

    @Override
    public List<BaseContactDTO> selectList(BaseContactDTO baseContactDTO) {
        BaseContactDO baseContactDO = buildCondition(baseContactDTO);
        return baseContactMapper.selectList(baseContactDO).stream().map(t -> CustomBeanUtils.baseContactDO2DTO(t)).collect(Collectors.toList());
    }

    @Override
    public PageData<List<BaseContactDTO>> selectList(BaseContactDTO baseContactDTO, Integer limit, Integer curr) {
        BaseContactDO baseContactDO = buildCondition(baseContactDTO);
        //return baseContactMapper.selectList(baseContactDO, new Page<>(curr, limit)).stream().map(t -> do2dto(t)).collect(Collectors.toList());
        Page<BaseContactDO> page = new Page<>(curr, limit);
        List<BaseContactDTO> baseContactDTOS = baseContactMapper.selectPageList(baseContactDO, page).stream().map(t -> CustomBeanUtils.baseContactDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>((int)page.getTotal(), baseContactDTOS);
    }

    private BaseContactDO buildCondition(BaseContactDTO baseContactDTO) {
//         .factoryCode("faccode")
//        .type("type")
//        .targetUid("targetuid")
//        .contactPerson("%person%")
//        .crmAddress("%add%")
//        .contactNumber("%num%")
//        .postcode("%post%")
//        .objuid("9d3703b7fc724f68bf7a41dac347080c")
        return BaseContactDO.builder()
                .factoryCode(CommonUtils.stringNull(baseContactDTO.getFactoryCode()))
                .type(CommonUtils.stringNull(baseContactDTO.getType()))
                .targetUid(CommonUtils.stringNull(baseContactDTO.getTargetUid()))
                .contactPerson(CommonUtils.warpStringNull(baseContactDTO.getContactPerson()))
                .crmAddress(CommonUtils.warpStringNull(baseContactDTO.getCrmAddress()))
                .contactNumber(CommonUtils.warpStringNull(baseContactDTO.getContactNumber()))
                .postcode(CommonUtils.warpStringNull(baseContactDTO.getPostcode()))
                .objuid(CommonUtils.stringNull(baseContactDTO.getObjuid()))
                .build();
    }
}
